/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icexam.util;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.ResourceBundle;

public class PropertyUtil {
    public PropertyUtil() {
    }
    private String getValueByPropertyName(String propertiesFileName,String propertyName) {
        String s="";
        Properties p=new Properties();
        FileInputStream in;
        try {
            //propertiesFileName如test.properties
            in = new FileInputStream(propertiesFileName);
            p.load(in);
            in.close();
            s=p.getProperty(propertyName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }
    private boolean changeValueByPropertyName(String propertiesFileName,String propertyName,String propertyValue) {
        boolean writeOK=true;
        Properties p=new Properties();
        FileInputStream in;
        try {

            in = new FileInputStream(propertiesFileName);
            p.load(in);//
            in.close();
            p.setProperty(propertyName,propertyValue);
            FileOutputStream out=new FileOutputStream(propertiesFileName);
            p.store(out,"Just Test");
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writeOK;
    }
    public static void changeIP(String ip){
        PropertyUtil operatePropertiesFile = new PropertyUtil();
        operatePropertiesFile.changeValueByPropertyName("csc.properties","IP",ip);
    }
    public static String getIP(){
        PropertyUtil operatePropertiesFile = new PropertyUtil();
        String value = operatePropertiesFile.getValueByPropertyName("csc.properties", "IP");
        return value;
    }
    public static void changePsw(String psw){
        PropertyUtil operatePropertiesFile = new PropertyUtil();
        operatePropertiesFile.changeValueByPropertyName("csc.properties","PSW",psw);
    }
    public static String getPsw(){
        PropertyUtil operatePropertiesFile = new PropertyUtil();
        String value = operatePropertiesFile.getValueByPropertyName("csc.properties", "PSW");
        return value;
    }


    public static void main(String[] args) {
        PropertyUtil.changeIP("122.123.22.123");
        System.out.println(PropertyUtil.getIP());
        String psw = "root";
        String p = PswUtil.getBASE64(psw);
        changePsw(p);
        String p2 =getPsw();
        System.out.println(PswUtil.getFromBASE64(p2));

    }
}